package com.wzz.server.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wzz.auth.cache.Cache;
import com.wzz.auth.cust.moudle.SendRedirect;
import com.wzz.auth.po.GetCodeParam;
import com.wzz.auth.po.GetTokenParam;
import com.wzz.auth.po.ReceCode;

@Controller
public class ServerControllerNew {
	
	@Autowired
	Cache cache;
	
	@RequestMapping("/server/a")
	public String accentRequest(GetCodeParam param, HttpServletRequest request, HttpServletResponse response) {
		String clientId = param.getClient_id();
		List<String> list = param.getScope();
		String state = param.getState();
		String url = param.getRedirect_uri();
		
		
			request.setAttribute("client_id", clientId);
			request.setAttribute("scope", list);
			request.setAttribute("state", state);
			request.setAttribute("redirect_uri", url);
			
			return "auth";
	}
	
	@RequestMapping("/deal/user")
	public void tongyi(String name, GetCodeParam param, HttpServletRequest request, HttpServletResponse response) {
		
		String clientId = param.getClient_id();
		List<String> list = param.getScope();
		String state = param.getState();
		String url = param.getRedirect_uri();
		
		SendRedirect sr = new SendRedirect(response);
		ReceCode recCode = new ReceCode();
		recCode.setState(state);
		
		String code = System.currentTimeMillis()+"";
		
		cache.getMap().put(code+"_code", code);
		cache.getMap().put(code, url);
		
		recCode.setCode(code);
		try {
			sr.sendRedirect(url, recCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("get/token")
	public void token(GetTokenParam param, HttpServletRequest request, HttpServletResponse response) {
		String code = param.getCode();
		String clientId = param.getClient_id();
		String url = param.getRedirect_uri();
		System.out.println("获取token");
		String token = "";
		if(code.equals(cache.getMap().get(code+"_code"))) {
			token = System.currentTimeMillis()+"";
			
		}else {
			token = "";
		}
		System.out.println("token="+token);
		try {
			response.sendRedirect(url+"?token="+token);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
